// 搭建 HTTP 服务
//  GET  /table   响应一个表格 

const data = [
    {
        id:1,
        song:'隐形的翅膀',
        name:'腾格尔'
    },
    {
        id:2,
        song:'孤勇者',
        name:'陈奕迅'
    },
    {
        id:3,
        song:'一生所爱',
        name:'周星驰'
    },
    {
        id:4,
        song:'爱如潮水',
        name:'张信哲'
    }
];

require('http').createServer((request,response)=>{
    // 定义响应体
    let body = `
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
            table{
                width:300px;
                height:300px;
            }
            table,td{
                border-collapse:collapse;
            }
        </style>
    </head>
    <body>
        <table border="1">
            <tr>
                <td>id</td>
                <td>歌手</td>
                <td>歌名</td>
            </tr>`
    // 将data数组中的数据，拼接到table表格中
    data.forEach(item=>{
        body += `<tr><td>${item.id}</td><td>${item.name}</td><td>${item.song}</td>`  
    })
    body += `</table>
        </body>
    </html>  
    `
    // 将拼好的html页面，设置到响应体中
    response.end(body);

}).listen(8080);


